/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmFacDetallada.java
 *
 * Created on 3/11/2011, 11:28:39 AM
 */
package api.form;

import api.dom.Factura;
import api.dom.Utilitaria;
import api.gest.GestFactura;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Luis
 */
public class FrmFacDetallada extends javax.swing.JInternalFrame {

    /** Creates new form FrmFacDetallada */
    public FrmFacDetallada() {
        initComponents();
        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
        jTable2.setModel(Utilitaria.listarFacturas((DefaultTableModel) jTable2.getModel()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        txtNombre1 = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnBuscar1 = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(720, 650));
        setMinimumSize(new java.awt.Dimension(720, 650));
        setNormalBounds(new java.awt.Rectangle(0, 0, 698, 581));
        setPreferredSize(new java.awt.Dimension(720, 650));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTable2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Fecha", "Cliente", "Observaciones", "Sub Total", "Mano de Obra", "Total"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable2MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTable2);

        getContentPane().add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 70, 720, 490));

        jPanel1.setBackground(new java.awt.Color(153, 180, 209));
        jPanel1.setMaximumSize(new java.awt.Dimension(720, 74));
        jPanel1.setMinimumSize(new java.awt.Dimension(720, 74));
        jPanel1.setPreferredSize(new java.awt.Dimension(720, 74));

        jLabel6.setBackground(new java.awt.Color(51, 255, 51));
        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 48));
        jLabel6.setText("Listado de Facturas");
        jLabel6.setToolTipText("");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addContainerGap(300, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 720, 74));

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        jPanel2.add(txtNombre1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 150, -1));

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 570, -1, 40));

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });
        getContentPane().add(btnBuscar, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 570, 90, 40));

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        getContentPane().add(btnEliminar, new org.netbeans.lib.awtextra.AbsoluteConstraints(620, 570, 100, 40));

        btnBuscar1.setText("...");
        btnBuscar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscar1ActionPerformed(evt);
            }
        });
        getContentPane().add(btnBuscar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 570, 30, 40));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseClicked



        if (evt.getClickCount() == 2) {
            String x = String.valueOf(this.jTable2.getValueAt(jTable2.getSelectedRow(), 0));
            int y = Integer.parseInt(x);
            Factura f = (Factura) GestFactura.getInstance().devolverFactura(y);
            new FrmDetalleDeDebe(f);
        }

}//GEN-LAST:event_jTable2MouseClicked

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        // TODO add your handling code here:
        try {
            DefaultTableModel modelo1 = (DefaultTableModel) jTable2.getModel();
            modelo1.setRowCount(0);
            int numfact = Integer.parseInt(txtNombre1.getText());
            Factura objF = GestFactura.getInstance().devolverFactura(numfact);
            Object[] objOp = {objF.getfCod(), objF.getfFecha(), objF.getObjCli().getCliNom(), objF.getfSubTotal(), (objF.getfTotal() - objF.getfSubTotal()), objF.getfTotal()};
            modelo1.addRow(objOp);
        } catch (NullPointerException e) {
            JOptionPane.showMessageDialog(rootPane, "No existe la factura que busca");
             jTable2.setModel(Utilitaria.listarFacturas((DefaultTableModel) jTable2.getModel()));
        }



}//GEN-LAST:event_btnBuscarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        // TODO add your handling code here:
        int numeroFact = Integer.parseInt(String.valueOf(jTable2.getValueAt(jTable2.getSelectedRow(), 0)));
        if (GestFactura.getInstance().bajaFactura(numeroFact) == true) {
            JOptionPane.showMessageDialog(rootPane, "Factura eliminada correctamente");
            jTable2.setModel(Utilitaria.listarFacturas((DefaultTableModel) jTable2.getModel()));
        } else {
            JOptionPane.showMessageDialog(rootPane, "No se puede eliminar factura. \n Algun pago esta asociado a ella");
        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnBuscar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscar1ActionPerformed
        // TODO add your handling code here:
        jTable2.setModel(Utilitaria.listarFacturas((DefaultTableModel) jTable2.getModel()));
        txtNombre1.setText("");
    }//GEN-LAST:event_btnBuscar1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnBuscar1;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField txtNombre1;
    // End of variables declaration//GEN-END:variables
}
